// pages/home/home.js
//引用同目录下的图片数据data.js
// var DBPost = require("../../db/DBPost.js").DBPost;
Page({

  data: {
    hideNotice: false,
    notice: "",
    marqueePace: 1,//滚动速度
    marqueeDistance: 10,//初始滚动距离
    size: 12,
    interval: 20, // 时间间隔
    countTime: '',
    huagong:[
      { 'img':'../../image/srcoll1.jpg'},
      { 'img': '../../image/srcoll2.jpg' },
      { 'img': '../../image/srcoll3.jpg' },
    ]
  },

  
  onLoad: function(options) {
    // var dbPost = new DBPost();
    // this.setData({
    //   huagong: dbPost.getAllPostData()
    // });
     getNotice(this);
  },

  
  onReady: function() {

  },


  //swiper点击事件进入详情页
  onSwiperTap: function(event) {
    var postId = event.target.dataset.postId;
    console.log(postId);
    wx.navigateTo({
      url: "detail/detail?id=" + postId,
    })
  },

//滚动函数
  run: function () {
    var _this = this;
    _this.data.countTime = setInterval(function () {
      if (-_this.data.marqueeDistance < _this.data.length) {
        _this.setData({
          marqueeDistance: _this.data.marqueeDistance - _this.data.marqueePace,
        });
      } else {
        clearInterval(_this.data.countTime);
        _this.setData({
          marqueeDistance: _this.data.windowWidth
        });
        _this.run();
      }
    }, _this.data.interval);
  },

});

const getNotice = function(_this){

  wx.request({
    url: 'https://dorm.dormassistant.wang:8080/notice/queryAllNotice',
    data: {
      currIndex: 1,
      pageSize: 2
    },
    method: "GET",
    success: function (res) {
      console.log(res.data.notices)
      _this.setData({
        notice: res.data.notices[0].tittle + ":  " + res.data.notices[0].text,
      });
      var length = _this.data.notice.length * _this.data.size; //文字长度
      var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
      _this.setData({ length, windowWidth });
      _this.setData({
        marqueeDistance: windowWidth
      });
      _this.run();
    },
    fail: function (res) {
      wx.showToast({
        title: '获取公告失败',
        icon: '',
        duration: 2000
      });
    }
  });

}